/**
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

package buses;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.io.HttpConnection;

/**
 * 
 * 
 * @author Dor Shemer 
 */
public class Line {
	
	/**
	 * Parses and returns the line number from a line data block
	 * 
	 * @param b
	 * @return
	 */
	public static int parseLineNumber(byte b[]) {
		return (b[0]<<8) + b[1];
	}
	
	public int lineNumber;
	public byte[] data;
	
	private Vector stations;
	private Vector departures;
	
	
	/**
	 * Creates a new Line from a given line data block
	 *
	 * @see parse(byte[])
	 * @param b
	 * @throws IOException
	 */
	public Line(byte[] b) throws IOException {
		parse(b);
	}
	
	/**
	 * Creates a new Line from the response of an already requested HttpConnection
	 * 
	 * @see parse(byte[])
	 * @param conn
	 * @param lineNumber
	 * @throws IOException
	 */
	public Line(HttpConnection conn, int lineNumber) throws IOException {
		int length = (int)conn.getLength();
		byte[] b = new byte[length+2];
		
		// adding the line number to the beggining of the line data block, to work with the parse method
		b[0] = (byte)(lineNumber>>8);
		b[1] = (byte)(lineNumber&0xff);
		
		conn.openDataInputStream().readFully(b, 2, length);
		parse(b);
	}
	
	
	/**
	 * Parses a line data block into a line.
	 * 
	 * A line data block is a byte array with the following structure:
	 * - a two byte big-endian number representing the line number
	 * - the names of all the starting stations, each delimited with a line break (\n)
	 * - another line break
	 * - the departing schedule from the corresponding station. each schedule is delimited with a line break. the schedules
	 * themselves are sets of four character time respresentations (i.e 001000300100 for 00:10, 00:30 and 1:00)
	 *  
	 * @param b
	 * @throws IOException
	 */
	private void parse(byte[] b) throws IOException {
		this.data = b;
		this.lineNumber = parseLineNumber(b);
		
		String s = new String(b, BusesMidlet.getInputEncoding());
		
		// two vectors to hold the stations and departures strings
		this.stations = new Vector();
		this.departures = new Vector();
		int i=2;
		
		Vector v = stations;
		
		// parsing the station and departure strings, starting with the stations and switching to strings when
		// finding an empty line
		for (int j; (j=s.indexOf('\n', i))>-1; i=j+1) {
			if (j==i) {
				// found an empty line, changing to the departure vector
				v = departures;
			} else {
				v.addElement(s.substring(i, j));
			}
		}
	}
	
	public Vector getStations() throws IOException {	
		return stations;
	}
	
	public String getStation(int index) {
		return index<stations.size() ? (String)stations.elementAt(index) : null;
	}	
	
	public Vector getDepartures(int index) {
		Vector v = null;
		if (index<departures.size()) {
			Object o = departures.elementAt(index);
			if (o instanceof String) {				
				// the departures vector holds an unparsed string, which means this is the first time the schedule
				// for this station was requested. parsing it
				String s = (String)o;
				int length = s.length()/4;
				v = new Vector(length);
				char[] carr = new char[5];
				carr[2] = ':';
				for (int i=0; i<length; i++) {
					for (int j=0; j<4; j++) {
						carr[j+j/2] = s.charAt(i*4+j);
					}
					v.addElement(new String(carr));
				}
				
				// replacing the original string with the now parsed schedule vector
				departures.setElementAt(v, index);
			} else {
				v = (Vector)o;
			}			
		}
		return v;
	}	

	/**
	 * Returns whether two Lines are equal 
	 */
	public boolean equals(Object o) {
		if (o instanceof Line) {
			Line line = (Line)o;
			if (line.lineNumber!=lineNumber || line.data.length!=data.length) {
				return false;
			}
			
			for (int i=0; i<data.length; i++) {
				if (line.data[i]!=data[i]) {
					return false;
				}
			}
			
			return true;
		}
		return super.equals(o);
	}
}